import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class BankcardOption:

    def __init__(self, driver):
        self.driver = driver

    def click_to_bankcard_option(self):
        time.sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()

    def edit_context(self, bank_name, card_type, card_id):
        time.sleep(2)
        type_xpath = '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[1]' if card_type == 1 else '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[2]'
        self.driver.find_element(By.XPATH, '//*[@id="cardBank_add_input"]').send_keys(bank_name)
        self.driver.find_element(By.XPATH, type_xpath).click()
        self.driver.find_element(By.XPATH, '//*[@id="cardNum_add_input"]').send_keys(card_id)

    def save_btn(self):
        wait = WebDriverWait(self.driver, 10)
        btn = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="bankCard_save_btn"]')))
        btn.click()

    def update_context(self, bank_name, card_type, card_id):
        time.sleep(2)
        type_xpath = '//*[@id="bankCardUpdateModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[1]' if (
                card_type == 1) else '//*[@id="bankCardUpdateModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[2]'
        ele1 = self.driver.find_element(By.XPATH, '//*[@id="cardBank_update_input"]')
        ele1.clear()
        ele1.send_keys(bank_name)

        self.driver.find_element(By.XPATH, type_xpath).click()
        ele2 = self.driver.find_element(By.XPATH, '//*[@id="cardNum_update_input"]')
        ele2.clear()
        ele2.send_keys(card_id)

    def update_btn(self):
        wait = WebDriverWait(self.driver, 10)
        btn = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="bankCard_update_btn"]')))
        btn.click()

    def add_card(self):
        self.driver.find_element(By.XPATH, '//*[@id="bankCard_add_modal_btn"]').click()

    def delete_card(self):
        self.driver.find_element(By.XPATH,
                                 '/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[2]').click()

    def make_sure_delete(self):
        self.driver.find_element(By.CSS_SELECTOR, '.jconfirm-buttons .btn.btn-primary').click()

    def cancel_delete(self):
        self.driver.find_element(By.CSS_SELECTOR, '.jconfirm-buttons .btn.btn-default').click()

    def click_edit(self):
        time.sleep(1)
        self.driver.find_element(By.XPATH,
                                 '/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[1]').click()

    def click_default(self):
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (By.XPATH, '/html/body/div/div/main/div/div/div[3]/div/div/div[1]/ul/li/button[3]'))).click()
